package com.vidaappv1;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class OrderConfirmation extends Activity {

    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();
    private static String url_check_order = "http://192.168.43.191/vida/check_order.php";
    
   
	TextView orderState;
	int orderStatus;
    // JSON Node names
    private static final String TAG_ORDERSTATE= "orderstate";
    private static final String TAG = "errors";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_confirmation);
		new OrderStatus().execute();
		
		Button accountinfobutton = (Button)findViewById(R.id.button1);

		accountinfobutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),AccountInfo.class));
			}
		});
		
		
		ImageView fbshare = (ImageView)findViewById(R.id.imageView2);
		fbshare.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getApplicationContext(),FBshare.class));
				//TODO: carry across all orders to FB share plugin
			}
		});
		
		TextView orderState = (TextView)findViewById(R.id.orderState);
		Log.e(TAG,"errors in app");
		String orderPreparing = "Order now being prepared...";
		try{
			orderState.setText(orderPreparing);
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.order_confirmation, menu);
		return true;
	}
	

	class OrderStatus extends AsyncTask<String,String,String>{
	
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			setContentView(R.layout.activity_order_confirmation);
 			orderState = (TextView)findViewById(R.id.orderState);

		}
	
		
		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
        
            params.add(new BasicNameValuePair("orderStatus","true"));
            params.add(new BasicNameValuePair("pid","5"));
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_check_order,
                    "POST", params);
 
            // check log cat fro response
            Log.d("Create Response", json.toString());
            while(true){
	            // check for success tag
	            try {
	            	json = jsonParser.makeHttpRequest(url_check_order,
	                        "POST", params);
	            	orderStatus = json.getInt(TAG_ORDERSTATE);
	            	if(orderStatus==1) break;
	                
	            } catch (JSONException e) {
	                e.printStackTrace();
	            }
	            try{
	            	Thread.sleep(2000);
	            }
	            catch(Exception e){
	            
	            }
            }
            return null;
		}


		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(orderStatus ==1){
				Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
	 			v.vibrate(300);
	        	orderState.setText("Order Ready! please collect."); 
			}
		}


	
	}

}
